package org.luxor.commons.workflow.listener;

import org.activiti.engine.delegate.event.ActivitiEvent;
import org.activiti.engine.delegate.event.ActivitiEventListener;
import org.activiti.engine.delegate.event.ActivitiEventType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

/**
 * 全局监听器
 *
 * @author Mr.yan  @date 2021/10/21
 */
public class GlobalEventListener implements ActivitiEventListener {
    private static final Logger log = LoggerFactory.getLogger(GlobalEventListener.class);

    private Map<ActivitiEventType, ActivitiEventHandler> handlers = new HashMap<>();

    public GlobalEventListener(Map<ActivitiEventType, ActivitiEventHandler> handlers) {
        if (handlers != null) {
            this.handlers = handlers;
        }
    }

    @Override
    public void onEvent(ActivitiEvent event) {
        ActivitiEventHandler eventHandler = handlers.get(event.getType());
        if (eventHandler == null) {
            log.trace("进入[工作流全局监听器(GlobalEventListener)],参数:{}", event);
        } else {
            eventHandler.handle(event);
        }
    }

    @Override
    public boolean isFailOnException() {
        return false;
    }
}
